Already did a straight function to get a random element from an array. It’s even easier as a prototype to extend the base Array object. Then you can just call the “random” method on any array.
<script type="text/javascript">//<![CDATA[
Array.prototype.random = function () {
return this[ Math.floor( Math.random() * this.length ) ];
}
var stuff = new Array('ant', 'grasshopper', 'scorpion', 'frog');
for ( i = 0 ; i < 10; i++ ) {
document.write( stuff.random() + ' ');
}
//]]>
</script>